package com.schoolcloud.serve.service.impl;

import com.schoolcloud.api.client.front.FrontUserClient;
import com.schoolcloud.common.model.NotificationRequest;
import com.schoolcloud.common.model.R;
import com.schoolcloud.common.model.UserInfo;
import com.schoolcloud.common.util.UserContext;
import com.schoolcloud.serve.dto.ItemRequestSaveDTO;
import com.schoolcloud.serve.entity.*;
import com.schoolcloud.serve.enums.RequestProcessActionType;
import com.schoolcloud.serve.mq.producer.NotificationProducer;
import com.schoolcloud.serve.service.*;
import com.schoolcloud.serve.vo.ItemRequestInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;

@Service
@RequiredArgsConstructor
@Slf4j
public class LostFoundInteractionServiceImpl implements LostFoundInteractionService {
    private final ItemRequestsService itemRequestsService;
    private final RequestProcessService requestProcessService;
    private final RequestVerificationsService requestVerificationsService;
    private final FoundItemsService foundService;
    private final LostItemsService lostService;
    private final NotificationProducer notificationProducer;

    private final FrontUserClient frontUserClient;

    @Override
    @Transactional
    public void saveItemRequest(ItemRequestSaveDTO dto) {
//        保存基本信息
        ItemRequests itemRequests = new ItemRequests();
        BeanUtils.copyProperties(dto, itemRequests);
        itemRequests.setStatus(RequestProcessActionType.PENDING.getCode());
        itemRequests.setCreatedTime(LocalDateTime.now());
        itemRequests.setUserId(UserContext.getUserToInteger());
        itemRequestsService.save(itemRequests);
//        保存验证信息
        RequestVerifications verifications = new RequestVerifications();
        BeanUtils.copyProperties(dto, verifications);
        verifications.setProofImages(String.join(",", dto.getProofImages()));
        verifications.setRequestId(itemRequests.getRequestId());
        requestVerificationsService.save(verifications);
        NotificationRequest request = new NotificationRequest();
        request.setSenderId(UserContext.getUserToInteger());
        HashMap<String, String> map = new HashMap<>();
        if (itemRequests.getRequestType() == 1) {
//               对方发布的是招领 申请人（失主）认领
            request.setType("owner_claim");
            FoundItems byId = foundService.getById(itemRequests.getItemId());
            request.setRelatedId(byId.getFoundItemId());
            request.setRelatedType("lost-item");
            request.setReceiverId(byId.getUserId());
            map.put("item_title", byId.getTitle());
        } else {
//             对方发布的是失物   申请人（拾主）归还
            log.info("id"+itemRequests.getItemId());
            LostItems byId = lostService.getById(itemRequests.getItemId());
            request.setType("finder_claim");
            request.setRelatedId(byId.getLostItemId());
            request.setRelatedType("found-item");
            request.setReceiverId(byId.getUserId());
            map.put("item_title", byId.getTitle());
        }
//        发送通知
        request.setVariables(map);
        notificationProducer.sendNotification(request);
    }

    @Override
    @Transactional
    public void requestProcess(RequestProcess requestProcess) {
        requestProcess.setActionTime(LocalDateTime.now());
        requestProcess.setProcessorId(UserContext.getUserToInteger());
        requestProcessService.save(requestProcess);
//        修改状态
        ItemRequests itemRequests = new ItemRequests();
        itemRequests.setRequestId(requestProcess.getRequestId());
        itemRequests.setStatus(requestProcess.getActionType());
        itemRequestsService.updateById(itemRequests);
    }

    @Override
    public ItemRequestInfoVO getItemRequestInfoById(Integer id) {
        ItemRequests byId = itemRequestsService.getById(id);
        ItemRequestInfoVO itemRequestInfoVO = new ItemRequestInfoVO();
        BeanUtils.copyProperties(byId, itemRequestInfoVO);
        RequestVerifications verifications = requestVerificationsService.getByQuestId(id);
        itemRequestInfoVO.setVerifications(verifications);
        itemRequestInfoVO.setProcessList(requestProcessService.getProcessListByRequestId(id));
        return itemRequestInfoVO;
    }
}
